﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MCMesServer.NetConnectFunc
{
    public class ConfigClass
    {
        #region API函数声明
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern long GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
        #endregion

        #region 读取INI文件
        public static string ReadIniData(string Section, string Key, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                byte[] temp = new byte[2048];
                long bufferLen = GetPrivateProfileString(Section, Key, "", temp, temp.GetUpperBound(0), iniFilePath);
              
                string a = Encoding.UTF8.GetString(temp, 0, (int)bufferLen);
                return a;
            }
            else
            {
                return string.Empty;
            }
        }
        #endregion

        #region 写入ini文件
        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        #endregion
    }
}
